package com.anhuanjia.tools.eao.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.util.StringUtils;

import com.anhuanjia.tools.eao.bean.EaoProjectInfo;
import com.anhuanjia.tools.eao.bean.EaoReportChapter;
import com.efastsoft.framework.Member;
import com.efastsoft.framework.UIFacade;
import com.efastsoft.framework.annotation.Alias;
import com.efastsoft.framework.annotation.Priv;
import com.efastsoft.framework.annotation.Priv.LoginType;
import com.efastsoft.framework.data.DataRow;
import com.efastsoft.framework.data.DataTable;
import com.efastsoft.framework.data.Q;
import com.efastsoft.framework.data.Transaction;
import com.efastsoft.framework.i18n.Lang;

@Alias(value = "reportchapter")
public class ReportChapterUI extends UIFacade {

	/**
	 * 添加或更改
	 */
	@Priv(loginType = LoginType.Member)
	public void saveOrUpdate() {
		Transaction trans = new Transaction();
		EaoReportChapter entity = new EaoReportChapter();
		entity.setValue(this.Request);
		String id = entity.getId();
		if (StringUtils.isEmpty(id)) {
			entity.setId(UUID.randomUUID().toString());
			trans.insert(entity);
			if (trans.commit()) {
				s(Lang.get("Common.ExecuteSuccess"));
			} else {
				f(Lang.get("Common.ExecuteFailed"));
			}
		} else {
			DataTable dt = new Q(" select * from eao_report_chapter where Id = ?", id).fetch();
			EaoReportChapter old = new EaoReportChapter();
			old.setValue(dt.get(0));
			old.setValue(this.Request);
			trans.update(old);
			if (trans.commit()) {
				s(Lang.get("Common.ExecuteSuccess"));
			} else {
				f(Lang.get("Common.ExecuteFailed"));
			}
		}
	}

	/**
	 * 根据Id删除一条记录
	 */
	@Priv(loginType = LoginType.Member)
	public void delete() {
		String id = $V("Id");
		if (StringUtils.isEmpty(id)) {
			f("操作失败，请确认操作信息");
			return;
		}
		Transaction trans = new Transaction();
		Q q2 = new Q(" delete from eao_report_chapter  where ").in("Id", id);
		trans.add(q2);
		if (trans.commit()) {
			s(Lang.get("Common.ExecuteSuccess"));
		} else {
			f(Lang.get("Common.ExecuteFailed"));
		}
	}

	/**
	 * 查询所有
	 */
	@Priv(loginType = LoginType.Member)
	public void findAll() {
		DataTable dt = new Q("select * from eao_report_chapter ").fetch();
		List<EaoReportChapter> list = new ArrayList<>();
		for (DataRow dr : dt) {
			EaoReportChapter entity = new EaoReportChapter();
			entity.setValue(dr);
			list.add(entity);
			this.Response.put("data", list);
		}
	}

	/**
	 * 根据id查询单个
	 */
	@Priv(loginType = LoginType.Member)
	public void findOne() {
		String searchKey = $V("SearchKey");
		if (StringUtils.isEmpty(searchKey)) {
			f("请输入搜索条件！");
			return;
		}
		Q q = new Q("select * from eao_report_chapter where Id = ? ", searchKey);
		$S("data", q.fetch());
	}
	
	
	/**
	 * 查询所有自选报告书、报告表,分配报告书、报告表章节
	 */
	@Priv(loginType = LoginType.Member)
	public void findByBookOrBook() {
		String project_id = $V("ProjectId");//获取项目id
		String Report_type = $V("ReportType");//获取报告类型
		Long userId = Member.getUID();
		Integer perjectLeader = 0;
		DataTable dt = new Q("select p.PerjectLeader from eao_project_info as p,Mh_Resource as r where p.PerjectLeader = r.Member and p.Id = ?",project_id).fetch();
		DataTable dt1 = new Q("select Item from eao_report_type where ReportId = ?",Report_type).fetch();
		if(dt.getRowCount()>0){
			for (DataRow dataRow : dt) {
				EaoProjectInfo projectInfo = new EaoProjectInfo();
				projectInfo.setValue(dataRow);
			    perjectLeader = projectInfo.getPerjectLeader();
			}
			if(Integer.parseInt(userId.toString()) == perjectLeader){
				//查询所有责任人分配章节
				DataTable dtTable = new Q("select * from eao_report_chapter where Type = ? and (Main = '1' or Main = '0' )" , dt1.getString(0 , 0)).fetch();
				List<EaoReportChapter> list = new ArrayList<EaoReportChapter>();
				for (DataRow dr : dtTable) {
					EaoReportChapter entity = new EaoReportChapter();
					entity.setValue(dr);
					list.add(entity);
					this.Response.put("dataTable", list);
				}
			}else{
				//查询所有自选报告书章节
				DataTable dtAllBook = new Q("select * from eao_report_chapter where Type = ? and Main = '0' " , dt1.getString(0 , 0)).fetch();
				List<EaoReportChapter> listdtAllBook = new ArrayList<>();
				for (DataRow dr : dtAllBook) {
					EaoReportChapter entity = new EaoReportChapter();
					entity.setValue(dr);
					listdtAllBook.add(entity);
					this.Response.put("dataAllBook", listdtAllBook);
				}
			}
		}
		
	}
	@Priv(loginType = LoginType.Member)
	public void update() {
		String id = $V("Id");
		String main = $V("Main");
		DataTable dataTable = new Q(" select * from eao_report_chapter where Id = ? " , id).fetch();
		EaoReportChapter chapter = new EaoReportChapter();
		chapter.setValue(dataTable.get(0));
		chapter.setMain(main);
		if(chapter.update()) {
			s("修改成功");
		} else {
			f("修改失败");
		}
	}
}
